package com.gosun;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class DataEncry {
    public static String changIDcard(String idcard) {

        String substring = "";
        if (idcard == null) {
            substring = "";
        } else if (idcard.length() >= 18) {
            substring = idcard.substring(0, 6) + "********" + idcard.substring(14, idcard.length());
        } else if (idcard.length() == 15) {
            substring = idcard.substring(0, 6) + "******" + idcard.substring(12, idcard.length());
        } else {
            substring = idcard;
        }
        return substring;
    }

    public static String changName(String name) {
        String substring = "";
        if (name == null) {
            substring = "";
        } else if (name == "") {
            substring = "";
        } else if (name.length() > 1) {
            String a = "";
            for (int i = 1; i < name.length(); i++) {
                a += "*";
            }
            substring = name.substring(0, 1) + a;
        }
        return substring;
    }

    public static String changPhone(String name) {
        String substring = "";
        if (name == null) {
            substring = "";
        } else if (name == "") {
            substring = "";
        } else if (name.length() == 11) {
            substring = name.substring(0, 3) + "****" + name.substring(7, name.length());
        } else if (name.length() > 3) {
            String a = "";
            for (int i = 1; i < name.length() - 2; i++) {
                a += "*";
            }
            substring = name.substring(0, 3) + a;
        } else {
            substring = name;
        }
        return substring;
    }

    /*
     * 地址加密
     * */
    public static String changAddress(String s) {
        String ss = "";
        if (s == null) {
            return "";
        }
        if (s.contains("村") || s.indexOf("村") != 0) {
            int a = s.indexOf("省");
            int b = s.indexOf("市");
            int c = s.indexOf("县");
            int d = s.indexOf("区");
            int e = s.indexOf("镇");
            int f = s.indexOf("乡");
            int g = s.indexOf("村");
            String[] s1 = s.split("村");
            for (int i = 0; i < s1[0].length(); i++) {
                if (i == a) {
                    ss += "省";
                } else if (i == b) {
                    ss += "市";
                } else if (i == c) {
                    ss += "县";
                } else if (i == d) {
                    ss += "区";
                } else if (i == e) {
                    ss += "镇";
                } else if (i == f) {
                    ss += "乡";
                } else {
                    ss += "*";
                }
            }
            if (s1.length >= 2) {
                ss = ss + "村" + s1[1];
            } else {
                ss = ss + "村";
            }

        } else if (s.contains("区") || s.indexOf("区") != 0) {
            int a = s.indexOf("省");
            int b = s.indexOf("市");
            int c = s.indexOf("县");
            int d = s.indexOf("区");
            int e = s.indexOf("镇");
            int f = s.indexOf("乡");
            int g = s.indexOf("村");
            String[] s1 = s.split("区");
            for (int i = 0; i < s1[0].length(); i++) {
                if (i == a) {
                    ss += "省";
                } else if (i == b) {
                    ss += "市";
                } else if (i == c) {
                    ss += "县";
                } else if (i == d) {
                    ss += "区";
                } else if (i == e) {
                    ss += "镇";
                } else if (i == f) {
                    ss += "乡";
                } else {
                    ss += "*";
                }
            }
            if (s1.length >= 2) {
                ss = ss + "区" + s1[1];
            } else {
                ss = ss + "区";
            }
        } else if (s.contains("县") || s.indexOf("县") != 0) {
            int a = s.indexOf("省");
            int b = s.indexOf("市");
            int c = s.indexOf("县");
            int d = s.indexOf("区");
            int e = s.indexOf("镇");
            int f = s.indexOf("乡");
            int g = s.indexOf("村");
            String[] s1 = s.split("县");

            for (int i = 0; i < s1[0].length(); i++) {
                if (i == a) {
                    ss += "省";
                } else if (i == b) {
                    ss += "市";
                } else if (i == c) {
                    ss += "县";
                } else if (i == d) {
                    ss += "区";
                } else if (i == e) {
                    ss += "镇";
                } else if (i == f) {
                    ss += "乡";
                } else {
                    ss += "*";
                }
            }
            if (s1.length >= 2) {
                ss = ss + "县" + s1[1];
            } else {
                ss = ss + "县";
            }

        } else if (s.contains("市") || s.indexOf("市") != 0) {
            int a = s.indexOf("省");
            int b = s.indexOf("市");
            int c = s.indexOf("县");
            int d = s.indexOf("区");
            int e = s.indexOf("镇");
            int f = s.indexOf("乡");
            int g = s.indexOf("村");
            String[] s1 = s.split("市");
            for (int i = 0; i < s1[0].length(); i++) {
                if (i == a) {
                    ss += "省";
                } else if (i == b) {
                    ss += "市";
                } else if (i == c) {
                    ss += "县";
                } else if (i == d) {
                    ss += "区";
                } else if (i == e) {
                    ss += "镇";
                } else if (i == f) {
                    ss += "乡";
                } else {
                    ss += "*";
                }
            }
            if (s1.length >= 2) {
                ss = ss + "市" + s1[1];
            } else {
                ss = ss + "市";
            }

        } else {
            ss = s;
        }

        return ss;
    }

    /*
     * 加密
     * */
    public static String changAES(String dt) {

        String key = "2314891998299746";
        String iv = "1234561234561234";
        try {
            if (dt == null) {
                return "";
            }
            String data = dt.replace(" ", "");
            Cipher cipher = null;
            cipher = Cipher.getInstance("AES/CBC/NoPadding");

            int blockSize = cipher.getBlockSize();
            //byte[] dataBytes = data1.getBytes();
            byte[] dataBytes = data.toString().getBytes();
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength += blockSize - plaintextLength % blockSize;
            }

            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

            byte[] encrypted = new byte[0];
            cipher.init(1, keyspec, ivspec);
            encrypted = cipher.doFinal(plaintext);
            String encrystring = (new Base64()).encodeToString(encrypted);
            String encry = encrystring.replaceAll("\r|\n", "");
            return new String(encry.getBytes(), "utf-8");
        } catch (Exception e) {
            return null;
        }
    }

    /*
     * 解密
     * */
    public static String changText(String data) {
        String key = "2314891998299746";
        String iv = "1234561234561234";
        try {
            if (data == null) {
                return null;
            }
            //byte[] encrypted1 = ParseCiphertext.decode(data);//先用base64解密
            String base64EncodedString = new String(data.getBytes(), "utf-8");
            byte[] decode = new Base64().decode(base64EncodedString);
            //byte[] decode = decode(data);
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
            byte[] original = cipher.doFinal(decode);
            String originalString = new String(original).trim();

            return originalString;
        } catch (Exception e) {
            return null;
        }
    }


}
